package kr.hyunist.androidcomplete01saeildamil.ch07;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.*;
import android.view.*;

public class BingoExercise extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyView mv = new MyView(this);
		setContentView(mv);

	}

	class MyView extends View {
		Paint pnt;
		int width;
		int height;
		BitmapDrawable bd;
		Bitmap bit;
		int num;
		int img[] = { R.drawable.num_1, R.drawable.num_2, R.drawable.num_3,
				R.drawable.num_4, R.drawable.num_5, R.drawable.num_6,
				R.drawable.num_7, R.drawable.num_8, R.drawable.num_9,
				R.drawable.num_10, R.drawable.num_11, R.drawable.num_12,
				R.drawable.num_13, R.drawable.num_14, R.drawable.num_15,
				R.drawable.num_16, R.drawable.num_17, R.drawable.num_18,
				R.drawable.num_19, R.drawable.num_20, R.drawable.num_21,
				R.drawable.num_22, R.drawable.num_23, R.drawable.num_24,
				R.drawable.num_25 };

		public MyView(Context context) {
			super(context);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.LTGRAY);
			pnt = new Paint(Paint.ANTI_ALIAS_FLAG);

			Display defaultDisplay = ((WindowManager) getSystemService(WINDOW_SERVICE))
					.getDefaultDisplay();

			HashSet<Integer> set = new HashSet<Integer>();

			while (set.size() < 25) {
				for (int j = 0; j < 5 && set.size() < 25; j++)
					set.add((int) (Math.random() * 25));
			}

			Iterator<Integer> it = set.iterator();

			Resources res = getResources();

			int width = 0;
			int height = 0;

			while (it.hasNext()) {
				num = it.next();
				bd = (BitmapDrawable) res.getDrawable(img[num]);
				bit = bd.getBitmap();
				canvas.drawBitmap(bit, width, height, pnt);

				/*BitmapFactory.Options option = new BitmapFactory.Options();
				option.inJustDecodeBounds = true;
				BitmapFactory.decodeResource(this.getResources(), img[num], option);*/

				width += bit.getWidth();
				
				if (width == (bit.getWidth() * 5)) {
					width = 0;
					height += bit.getHeight();
				}

			}

			pnt.setColor(Color.RED);
			pnt.setStrokeWidth(3);
			canvas.drawLine(255, 0, 255, 360, pnt);
			
			pnt.setColor(Color.RED);
			pnt.setStrokeWidth(3);
			canvas.drawLine(360, 0, 0, 360, pnt);
		
			pnt.setColor(Color.BLACK);
			pnt.setStrokeWidth(3);
			canvas.drawLine(0, 0, 360, 360, pnt);
		}

	}
}
